<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PixelSearch</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>PixelSearch</h1>

<p>Searches a region of the screen for a pixel of the specified color.</p>

<pre class="Syntax">PixelSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ColorID [, Variation, Fast|RGB]</pre>
<h3>Parameters</h3>
<dl>

  <dt>OutputVarX/Y</dt>
  <dd><p>The names of the variables in which to store the X and Y coordinates of the first pixel that matches <em>ColorID</em> (if no match is found, the variables are made blank). Coordinates are relative to the active window unless <a href="CoordMode.htm">CoordMode</a> was used to change that.</p>
    <p>Either or both of these parameters may be left blank, in which case ErrorLevel (see below) can be used to determine whether a match was found.</p></dd>

  <dt>X1, Y1</dt>
  <dd><p>The X and Y coordinates of the upper left corner of the rectangle to search, which can be <a href="../Variables.htm#Expressions">expressions</a>. <strong>Coordinates are relative to the active window unless <a href="CoordMode.htm">CoordMode</a> was used to change that</strong>.</p></dd>

  <dt>X2, Y2</dt>
  <dd><p>The X and Y coordinates of the lower right corner of the rectangle to search, which can be <a href="../Variables.htm#Expressions">expressions</a>. Coordinates are relative to the active window unless <a href="CoordMode.htm">CoordMode</a> was used to change that.</p></dd>

  <dt>ColorID</dt>
  <dd><p>The decimal or hexadecimal color ID to search for, in Blue-Green-Red (BGR) format, which can be an <a href="../Variables.htm#Expressions">expression</a>. Color IDs can be determined using Window Spy (accessible from the tray menu) or via <a href="PixelGetColor.htm">PixelGetColor</a>. For example: <code>0x9d6346</code>.</p></dd>

  <dt>Variation</dt>
  <dd><p>A number between 0 and 255 (inclusive) to indicate the allowed number of shades of variation in either direction for the intensity of the red, green, and blue components of the color (can be an <a href="../Variables.htm#Expressions">expression</a>). This parameter is helpful if the color sought is not always exactly the same shade. If you specify 255 shades of variation, all colors will match. The default is 0 shades.</p></dd>

  <dt>Fast|RGB</dt>
  <dd><p>This parameter may contain the word Fast, RGB, or both (if both are present, separate them with a space; that is, <em>Fast RGB</em>).</p>
      <p><strong><a name="fast"></a>Fast</strong>: Uses a faster searching method that in most cases dramatically reduces the amount of CPU time used by the search. Although color depths as low as 8-bit (256-color) are supported, the fast mode performs much better in 24-bit or 32-bit color. If the screen's color depth is 16-bit or lower, the <em>Variation</em> parameter might behave slightly differently in fast mode than it does in slow mode. Finally, the fast mode searches the screen row by row (top down) instead of column by column. Therefore, it might find a different pixel than that of the slow mode if there is more than one matching pixel.</p>
      <p><strong>RGB</strong>: Causes <em>ColorID</em> to be interpreted as an RGB value instead of BGR. In other words, the red and blue components are swapped.</p></dd>

</dl>

<h3>ErrorLevel</h3>
<p><span class="ver">[v1.1.04+]</span> This command is able to throw an exception if there was a problem while searching. For more information, see <a href="Catch.htm#RuntimeErrors">Runtime Errors</a>.</p>
<p><a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to 0 if the color was found in the specified region, 1 if it was not found, or 2 if there was a problem that prevented the command from conducting the search.</p>
<h3>Remarks</h3>
<p>The region to be searched must be visible; in other words, it is not possible to  search a region of a window hidden behind another window. By contrast, pixels beneath the mouse cursor can usually be detected. The exception to this is cursors in games, which in most cases will hide any pixels beneath them.</p>
<p>Slow mode only: By default, the search starts at the upper-left pixel of the region and checks all pixels vertically beneath it for a match. If no match is found there, the search continues to the right, column by column, until it finds a matching pixel. The default left-to-right search order can be inverted by swapping <em>X1</em> and <em>X2</em> in the parameter list. In other words, if <em>X1</em> is greater than <em>X2</em>, the search will be conducted from right to left, starting at column <em>X1</em>. Similarly, if <em>Y1</em> is greater than <em>Y2</em>, each column of pixels to be searched starting at the bottom rather than the top. Finally, if the region to be searched is large and the search is repeated with high frequency, it may consume a lot of CPU time. To alleviate this, keep the size of the area to a minimum.</p>
<h3>Related</h3>
<p><a href="PixelGetColor.htm">PixelGetColor</a>, <a href="ImageSearch.htm">ImageSearch</a>, <a href="CoordMode.htm">CoordMode</a>, <a href="MouseGetPos.htm">MouseGetPos</a></p>
<h3>Example</h3>
<pre class="NoIndent">PixelSearch, Px, Py, 200, 200, 300, 300, 0x9d6346, 3, Fast
if ErrorLevel
    MsgBox, That color was not found in the specified region.
else
    MsgBox, A color within 3 shades of variation was found at X%Px% Y%Py%.</pre>

</body>
</html>
